<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_dbc.class.php');


class CI_Create_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('type', Tangra_Parameter_Method::GET, 'type'), 'type_event');
	}


	protected function type_event($ci_type_id) {
		if ($ci_type_id) {
			$dbc = $this->get_dbc();

			$type = new TCMS_CI_Type_DBC();
			if (!$type->load_by_id($dbc, $ci_type_id)) {
				throw new Tangra_Module_Exception('Cannot load TCMS_CI_Type_DBC with id = '.$ci_type_id);
			}

			$tvm = $this->get_tvm();
			if (!$tvm->is_page_var_registered('cis', 'last_type')) {
				$tvm->register_page_var('cis', 'last_type');
			}
			$tvm->set_page_var('cis', 'last_type', $ci_type_id);

			$view = $this->create_redirect_view($type->get_page_edit_path(), array($type->get_capture_prefix().'_add_new' => 1));
		} else {
			$view = $this->create_redirect_view('admin/modules/tangra_cms/ci/cis.php');
		}

		return $view;
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Expecting "type" parameter.');
	}
}
